package com.lv.service.ui

import android.app.Activity
import android.view.KeyEvent
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.lv.common.base.BaseActivity
import com.lv.common.ktx.LoadSrc
import com.lv.common.utils.ARoutUtils
import com.lv.common.utils.StatusBarUtil
import com.lv.service.R
import com.lv.service.databinding.ActivitySwitchCommonBinding
import kotlin.system.exitProcess

/**
 * 作者： ygx
 * 创建日期：2023/5/6 1:35 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：公共 开关页面
 */
@Route(path = ARoutUtils.SwitchCommonActivity)
class SwitchCommonActivity : BaseActivity<ActivitySwitchCommonBinding>(),
    View.OnClickListener {
    @Autowired(name = "title")//标题
    @JvmField
    var mTitle: String = ""

    @Autowired(name = "content")//内容
    @JvmField
    var mContent: String = ""

    @Autowired(name = "dec")//描述
    @JvmField
    var mDec: String = ""

    @Autowired(name = "isOpen")//开关状态
    @JvmField
    var mIsOpen: Boolean = false

    override fun getLayoutRes() = R.layout.activity_switch_common
    override fun initView() {
        super.initView()
        initStatusBar(this, true, true)
        ARouter.getInstance().inject(this)
        mBinding.apply {
            StatusBarUtil.setHeightAndPadding(this@SwitchCommonActivity, titleBar)
            titleBar.onClickBackIcon { exit() }
            ivSwitch.setOnClickListener(this@SwitchCommonActivity)
        }
    }


    override fun initConfig() {
        super.initConfig()
        mBinding.titleBar.setTitle(mTitle)
        mBinding.tvTitleSub.text = mContent
        mBinding.tvDec.text = mDec
        mBinding.ivSwitch.LoadSrc(if (mIsOpen) com.lv.common.R.drawable.icon_blue_switch_open else com.lv.common.R.drawable.icon_switch_off)

    }


    override fun initData() {
        super.initData()

    }

    override fun initEvents() {
        super.initEvents()

    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.iv_switch -> {
                mIsOpen = !mIsOpen
                mBinding.ivSwitch.LoadSrc(if (mIsOpen) com.lv.common.R.drawable.icon_blue_switch_open else com.lv.common.R.drawable.icon_switch_off)
            }


        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
        exitProcess(0)
    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit()
            return false
        }
        return super.onKeyDown(keyCode, event)
    }

    private fun exit() {
        intent.putExtra("isOpen", mIsOpen)
        setResult(Activity.RESULT_OK, intent)
        finish()
    }
}
